<?php
/**
 * 工程案例
 *
 * Author: Zeon
 * Date: 2018/9/1
 * Created by Panxsoft.
 */
namespace App\Http\Controllers\Home;

use App\Http\Models\Article;
use App\Http\Models\ArticleTag;
use App\Http\Requests\Home\ProjectCaseIndexRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use App\Http\Controllers\Controller;

class ProjectCaseController extends Controller
{
    use ApiResponser,ApiChecker;

    /**
     * @param ProjectCaseIndexRequest $request
     * @return array|\Illuminate\Http\Response
     */
    public function index(ProjectCaseIndexRequest $request)
    {
        $sub_types = $this->getSubCategory();

        // 返回解决方案所有的文章
        $sub_type_ids = $sub_types->pluck('id')->all();
        $articles = Article::filterArticleTypeId($request->article_type_id)
            ->whereIn('article_type_id', $sub_type_ids)
            ->latest('sort')
            ->limit(5)
            ->paginate($request->per_page?:15);

        return $this->response(compact('articles'));
    }

    /**
     * @return array|\Illuminate\Http\Response
     */
    public function create()
    {
        $sub_types = $this->getSubCategory();

        $sub_types = $sub_types->pluck('name', 'id')->all();

        return $this->response(compact('sub_types'));
    }

    /**
     * @return mixed
     */
    private function getSubCategory()
    {
        /** @var ArticleTag $solution_case */
        $solution_case = ArticleTag::where('id', ArticleTag::PROJECT_CASE)->first();
        $this->checkItemExist($solution_case, '解决方案');

        // 返回解决方案的二级菜单
        $sub_types = $solution_case->tags()
            ->latest('sort')
            ->get();

        return $sub_types;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function returnView()
    {
        return view("official_website.project_case");
    }
}
